package com.yc.cangst.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.cangst.model.EbNews;
import com.yc.cangst.model.EbProduct;
import com.yc.cangst.model.EbProductCategory;
import com.yc.cangst.service.EbNewsService;
import com.yc.cangst.service.EbProductCategorySercie;
import com.yc.cangst.service.EbProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller//迎宾证
@RequestMapping("/front")//工号

public class IndexController {

    @Autowired
    private EbProductService ebProductService;

    @Autowired
    private EbNewsService ebNewsService;

    @Autowired
    EbProductCategorySercie ebProductCategorySercie;

    @RequestMapping("/index")
    public String index(Model model, HttpSession session,
                        @RequestParam(value = "pSize",defaultValue = "1") Integer pSize,
                        @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize) {
        //1.调用service层查询分页的商品信息
        Page<EbProduct> ebProductPage = ebProductService.selectList(pSize, pageSize);

        //2.调用service层查询分页的新闻信息
        List<EbNews> ebNewsList = ebNewsService.selectList(1, 6);


        int maxPage = ebProductService.getMaxPage(pageSize);

        Map<String, List<EbProductCategory>> categoryMap = ebProductCategorySercie.selectBigAndSmallCate();

        model.addAttribute("curPage",pSize);

        model.addAttribute("ebProductList",ebProductPage.getRecords());

        model.addAttribute("ebNewsList",ebNewsList);

        model.addAttribute("categoryMap", categoryMap);

        model.addAttribute("ebProductPage",ebProductPage);

        model.addAttribute("maxPage",maxPage);

        return "index";
    }
//    @RequestMapping("/detail")
//    public String detail(Model model, Integer epId) {
//        //1.调用service查询该商品的详细信息
//        EbProduct productDetail = ebProductService.detail(epId);
//
//        model.addAttribute("detail", productDetail);
//
//        return "index";//templates/product-view.html
//    }
}
